package com.redis.service;

import com.github.pagehelper.PageHelper;
import com.redis.dao.CityMapper;
import com.redis.dao.DeviceMapper;
import com.redis.dao.UserMapper;
import com.redis.entity.City;
import com.redis.entity.Device;
import com.redis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DeviceMapper deviceMapper;


    @Override
    public int addUser(User user) {

        return userMapper.insertSelective(user);
    }

    @Override
    @Cacheable(value = "all", keyGenerator = "wiselyKeyGenerator")
    public List<User> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectAllUser();
    }

    @Override
    @Cacheable(value = "addresscache", keyGenerator = "wiselyKeyGenerator")
    public User selectByPrimaryKey(String id) {
        //调用数据库
        System.out.println("第一次调用显示");
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "allCitys", keyGenerator = "wiselyKeyGenerator")
    public List<City> findAll() {
        //调用数据库
        System.out.println("第一次调用显示");
        return cityMapper.selectAllCity();
    }

    @Override
    @Cacheable(value = "allDevice", keyGenerator = "wiselyKeyGenerator")
    public List<Device> findAllDevice() {
        //调用数据库
        System.out.println("第一次调用显示");
        return deviceMapper.selectAll();
    }

    @Override
//    @Cacheable(value = "Device", keyGenerator = "wiselyKeyGenerator")
    public List<Device> findDevice(String deviceId) {
        //调用数据库
        System.out.println("第一次调用显示");
        return deviceMapper.selectByDeviceId(deviceId);
    }

    @Override
    public List<City> findCityByPopulation(int population) {
        return cityMapper.findCityByPopulation(population);
    }


}
